<template>
  <div class="login-container">
    <div class="login-form">
      <h2 class="title">校园活动管理系统</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="loginForm.remember" label="记住密码" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
            round
            :disabled="loading"
          >
            登录
          </el-button>
        </el-form-item>
        <div class="links">
          <router-link to="/register">立即注册</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'
import { login } from '../api/user'

export default {
  name: 'LoginPage',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        remember: false
      },
      loading: false,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少为 6 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 检查是否有记住的密码
    const savedUsername = localStorage.getItem('rememberedUsername')
    const savedPassword = localStorage.getItem('rememberedPassword')
    if (savedUsername && savedPassword) {
      this.loginForm.username = savedUsername
      this.loginForm.password = savedPassword
      this.loginForm.remember = true
    }
  },
  methods: {
    async handleLogin() {
      try {
        await this.$refs.loginFormRef.validate()
        this.loading = true
        
        // 直接调用登录API
        const response = await login({
          username: this.loginForm.username,
          password: this.loginForm.password
        })
        
        // 处理登录成功
        if (response.success) {
          // 保存token到localStorage
          localStorage.setItem('token', response.token)
          localStorage.setItem('userInfo', JSON.stringify(response.user))
          
          // 记住密码
          if (this.loginForm.remember) {
            localStorage.setItem('rememberedUsername', this.loginForm.username)
            localStorage.setItem('rememberedPassword', this.loginForm.password)
          } else {
            localStorage.removeItem('rememberedUsername')
            localStorage.removeItem('rememberedPassword')
          }
          
          ElMessage.success('登录成功')
          this.$router.push('/')
        } else {
          ElMessage.error(response.message || '登录失败，请检查用户名和密码')
        }
      } catch (error) {
        ElMessage.error(error.response?.data?.message || error.message || '登录失败，请检查用户名和密码')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-form {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  color: #303133;
  margin-bottom: 30px;
  font-size: 24px;
  font-weight: 600;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}

.links {
  text-align: center;
  margin-top: 20px;
}

.links a {
  color: #409eff;
  text-decoration: none;
}

.links a:hover {
  color: #66b1ff;
}

@media (max-width: 480px) {
  .login-form {
    padding: 24px;
  }
}
</style>